package net.sahv.bdyz.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

public class UPloadUtil {
	/**
	 * 上穿图片
	 * @param file
	 * @param request
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public String upload(MultipartFile file, HttpServletRequest request) throws IllegalStateException, IOException {
		String myFileName = file.getOriginalFilename();
		String fileName = System.currentTimeMillis() + (int) (Math.random() * 10000)
				+ myFileName.substring(myFileName.lastIndexOf("."));
		// 定义上传路径
		String path = request.getServletContext().getRealPath("/upload/" + fileName);
		File localFile = new File(path);
		file.transferTo(localFile);
		return fileName;
	}
	/**
	 * 删除图片
	 * @param fileName
	 * @param request
	 * @return
	 */
	public String del(String fileName,HttpServletRequest request){
		File file = new File(request.getServletContext().getRealPath("/upload/" + fileName));
		if(file.delete()){
			return "success";
		}
		return "false";
	}
	public String upload(HttpServletRequest request) {
		List<String> filenames = new ArrayList<String>();
		 //创建一个通用的多部分解析器  
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
        //判断 request 是否有文件上传,即多部分请求  
        if(multipartResolver.isMultipart(request)){  
            //转换成多部分request    
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
            //取得request中的所有文件名  
            Iterator<String> iter = multiRequest.getFileNames();  
            while(iter.hasNext()){  
            	
                //取得上传文件  
                MultipartFile file = multiRequest.getFile(iter.next());  
                if(file != null){  
                    //取得当前上传文件的文件名称  
                    String myFileName = file.getOriginalFilename();  
                    //如果名称不为“”,说明该文件存在，否则说明该文件不存在  
                    if(myFileName.trim() !=""){  
                        System.out.println(myFileName);  
                        //重命名上传后的文件名  
                        String fileName = System.currentTimeMillis()+(int)(Math.random()*10000)+myFileName.substring(myFileName.lastIndexOf("."));  
                        //定义上传路径  
                        String path = request.getServletContext().getRealPath("/upload/"+fileName) ;  
                        File localFile = new File(path);  
                        try {
							file.transferTo(localFile);
							filenames.add(fileName);
						} catch (IllegalStateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}  
                    }  
                }  
                
               
            }  
        }
        StringBuffer sb = new StringBuffer();
			for(String it:filenames){
				sb.append(it+",");
			}
			if(sb.length()>0){
				return sb.substring(0, sb.length()-1);
			}
			return null;
	
	}
	public String upload1(MultipartFile[] file1,HttpServletRequest request) {
		List<String> filenames = new ArrayList<String>();
		
                if(file1.length>0){ 
                	for(MultipartFile file:file1){
                    //取得当前上传文件的文件名称  
                    String myFileName = file.getOriginalFilename();  
                    //如果名称不为“”,说明该文件存在，否则说明该文件不存在  
                    if(myFileName.trim() !=""){  
                        System.out.println(myFileName);  
                        //重命名上传后的文件名  
                        String fileName = System.currentTimeMillis()+(int)(Math.random()*10000)+myFileName.substring(myFileName.lastIndexOf("."));  
                        //定义上传路径  
                        String path = request.getServletContext().getRealPath("/upload/"+fileName) ;  
                        File localFile = new File(path);  
                        try {
							file.transferTo(localFile);
							filenames.add(fileName);
						} catch (IllegalStateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}  
                    }  
                }  
                
               
            }  
        
        StringBuffer sb = new StringBuffer();
			for(String it:filenames){
				sb.append(it+",");
			}
			
			if(sb.length()>0){
				return sb.toString();
			}
			return null;
	}
}
